<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Amf_Server - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.amf.server.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.amf.server.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.amf.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.amf.html">Zend_Amf</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.html">Zend_Application</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.amf.server" class="section"><div class="info"><h1 class="title">Zend_Amf_Server</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Amf_Server</span> provides an <acronym class="acronym">RPC</acronym>-style server for
        handling requests made from the Adobe Flash Player using the <acronym class="acronym">AMF</acronym>
        protocol. Like all Zend Framework server classes, it follows the SoapServer
        <acronym class="acronym">API</acronym>, providing an easy to remember interface for creating servers.
    </p>

    <div class="example" id="zend.amf.server.basic"><div class="info"><p><b>Example #1 Basic AMF Server</b></p></div>
        

        <div class="example-contents"><p>
            Let&#039;s assume that you have created a class <span class="classname">Foo</span> with a
            variety of public methods. You may create an <acronym class="acronym">AMF</acronym> server using the
            following code:
        </p></div>

        <pre class="programlisting brush: php">
$server = new Zend_Amf_Server();
$server-&gt;setClass(&#039;Foo&#039;);
$response = $server-&gt;handle();
echo $response;
</pre>


        <div class="example-contents"><p>
            Alternately, you may choose to attach a simple function as a
            callback instead:
        </p></div>

        <pre class="programlisting brush: php">
$server = new Zend_Amf_Server();
$server-&gt;addFunction(&#039;myUberCoolFunction&#039;);
$response = $server-&gt;handle();
echo $response;
</pre>


        <div class="example-contents"><p>
            You could also mix and match multiple classes and functions. When
            doing so, we suggest namespacing each to ensure that no method name
            collisions occur; this can be done by simply passing a second string
            argument to either  <span class="methodname">addFunction()</span> or
             <span class="methodname">setClass()</span>:
        </p></div>

        <pre class="programlisting brush: php">
$server = new Zend_Amf_Server();
$server-&gt;addFunction(&#039;myUberCoolFunction&#039;, &#039;my&#039;)
       -&gt;setClass(&#039;Foo&#039;, &#039;foo&#039;)
       -&gt;setClass(&#039;Bar&#039;, &#039;bar&#039;);
$response = $server-&gt;handle();
echo $response;
</pre>


        <div class="example-contents"><p>
            The <span class="classname">Zend_Amf_Server</span> also allows services to be dynamically
            loaded based on a supplied directory path. You may add as many directories as you wish
            to the server. The order that you add the directories to the server will be the
            order that the <acronym class="acronym">LIFO</acronym> search will be performed on the directories to
            match the class. Adding directories is completed with the
             <span class="methodname">addDirectory()</span> method.
        </p></div>

        <pre class="programlisting brush: php">
$server-&gt;addDirectory(dirname(__FILE__) .&#039;/../services/&#039;);
$server-&gt;addDirectory(dirname(__FILE__) .&#039;/../package/&#039;);
</pre>


        <div class="example-contents"><p>
            When calling remote services your source name can have underscore (&quot;_&quot;) and dot (&quot;.&quot;)
            directory delimiters. When an underscore is used <acronym class="acronym">PEAR</acronym> and Zend
            Framework class naming conventions will be respected. This means that if you call the
            service com_Foo_Bar the server will look for the file
            <var class="filename">Bar.php</var> in the each of the included paths at
            <var class="filename">com/Foo/Bar.php</var>. If the dot notation is used for your remote
            service such as <var class="filename">com.Foo.Bar</var> each included path will have
            <var class="filename">com/Foo/Bar.php</var> append to the end to autoload
            <var class="filename">Bar.php</var>
        </p></div>

        <div class="example-contents"><p>
            All <acronym class="acronym">AMF</acronym> requests sent to the script will then be handled by the
            server, and an <acronym class="acronym">AMF</acronym> response will be returned.
        </p></div>
    </div>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>All Attached Methods and Functions Need Docblocks</b><br /></span>
        

        <p class="para">
            Like all other server components in Zend Framework, you must document your class
            methods using <acronym class="acronym">PHP</acronym> docblocks. At the minimum, you
            need to provide annotations for each required argument as well as
            the return value. As examples:
        </p>

        <pre class="programlisting brush: php">
// Function to attach:

/**
 * @param  string $name
 * @param  string $greeting
 * @return string
 */
function helloWorld($name, $greeting = &#039;Hello&#039;)
{
    return $greeting . &#039;, &#039; . $name;
}
</pre>


        <pre class="programlisting brush: php">
// Attached class

class World
{
    /**
     * @param  string $name
     * @param  string $greeting
     * @return string
     */
    public function hello($name, $greeting = &#039;Hello&#039;)
    {
        return $greeting . &#039;, &#039; . $name;
    }
}
</pre>


        <p class="para">
            Other annotations may be used, but will be ignored.
        </p>
    </p></blockquote>

    <div class="section" id="zend.amf.server.flex"><div class="info"><h1 class="title">Connecting to the Server from Flex</h1></div>
        

        <p class="para">
            Connecting to your <span class="classname">Zend_Amf_Server</span> from your Flex
            project is quite simple; you simply need to point your endpoint <acronym class="acronym">URI</acronym>
            to your <span class="classname">Zend_Amf_Server</span> script.
        </p>

        <p class="para">
            Say, for instance, you have created your server and placed it in the
            <var class="filename">server.php</var> file in your application root, and thus the
            <acronym class="acronym">URI</acronym> is <var class="filename">http://example.com/server.php</var>. In this
            case, you would modify your <var class="filename">services-config.xml</var> file to set the
            channel endpoint uri attribute to this value.
        </p>

        <p class="para">
            If you have never created a <var class="filename">service-config.xml</var> file you can do so
            by opening your project in your Navigator window. Right click on the project name and
            select &#039;properties&#039;. In the Project properties dialog go into &#039;Flex Build Path&#039; menu,
            &#039;Library path&#039; tab and be sure the &#039;<var class="filename">rpc.swc</var>&#039; file is added to your
            projects path and Press Ok to close the window.
        </p>

        <p class="para">
            You will also need to tell the compiler to use the
            <var class="filename">service-config.xml</var> to find the RemoteObject endpoint. To do this
            open your project properties panel again by right clicking on the project folder from
            your Navigator and selecting properties. From the properties popup select &#039;Flex
            Compiler&#039; and add the string: <strong class="command">-services &quot;services-config.xml&quot;</strong>. Press
            Apply then OK to return to update the option. What you have just done is told the Flex
            compiler to look to the <var class="filename">services-config.xml</var> file for runtime
            variables that will be used by the RemotingObject class.
        </p>

        <p class="para">
            We now need to tell Flex which services configuration file to use for connecting to
            our remote methods. For this reason create a new
            &#039;<var class="filename">services-config.xml</var>&#039; file into your Flex project src folder. To
            do this right click on the project folder and select &#039;new&#039; &#039;File&#039; which will popup a
            new window. Select the project folder and then name the file
            &#039;<var class="filename">services-config.xml</var>&#039; and press finish.
        </p>

        <p class="para">
            Flex has created the new <var class="filename">services-config.xml</var> and has it open. Use
            the following example text for your <var class="filename">services-config.xml</var> file. Make
            sure that you update your endpoint to match that of your testing server. Make sure you
            save the file.
        </p>

        <pre class="programlisting brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;services-config&gt;
    &lt;services&gt;
        &lt;service id=&quot;zend-service&quot;
            class=&quot;flex.messaging.services.RemotingService&quot;
            messageTypes=&quot;flex.messaging.messages.RemotingMessage&quot;&gt;
            &lt;destination id=&quot;zend&quot;&gt;
                &lt;channels&gt;
                    &lt;channel ref=&quot;zend-endpoint&quot;/&gt;
                &lt;/channels&gt;
                &lt;properties&gt;
                    &lt;source&gt;*&lt;/source&gt;
                &lt;/properties&gt;
            &lt;/destination&gt;
        &lt;/service&gt;
    &lt;/services&gt;
    &lt;channels&gt;
        &lt;channel-definition id=&quot;zend-endpoint&quot;
            class=&quot;mx.messaging.channels.AMFChannel&quot;&gt;
            &lt;endpoint uri=&quot;http://example.com/server.php&quot;
                class=&quot;flex.messaging.endpoints.AMFEndpoint&quot;/&gt;
        &lt;/channel-definition&gt;
    &lt;/channels&gt;
&lt;/services-config&gt;
</pre>


        <p class="para">
            There are two key points in the example. First, but last in the
            listing, we create an <acronym class="acronym">AMF</acronym> channel, and specify the endpoint as the
            <acronym class="acronym">URL</acronym> to our <span class="classname">Zend_Amf_Server</span>:
        </p>

        <pre class="programlisting brush: xml">
&lt;channel-definition id=&quot;zend-endpoint&quot;
    &lt;endpoint uri=&quot;http://example.com/server.php&quot;
        class=&quot;flex.messaging.endpoints.AMFEndpoint&quot;/&gt;
&lt;/channel-definition&gt;
</pre>


        <p class="para">
            Notice that we&#039;ve given this channel an identifier, &quot;zend-endpoint&quot;.
            The example create a service destination that refers to this channel,
            assigning it an ID as well -- in this case &quot;zend&quot;.
        </p>

        <p class="para">
            Within our Flex <acronym class="acronym">MXML</acronym> files, we need to bind a RemoteObject to the
            service. In <acronym class="acronym">MXML</acronym>, this might be done as follows:
        </p>

        <pre class="programlisting brush: xml">
&lt;mx:RemoteObject id=&quot;myservice&quot;
    fault=&quot;faultHandler(event)&quot;
    showBusyCursor=&quot;true&quot;
    destination=&quot;zend&quot;&gt;
</pre>


        <p class="para">
            Here, we&#039;ve defined a new remote object identified by &quot;myservice&quot;
            bound to the service destination &quot;zend&quot; we defined in the
            <var class="filename">services-config.xml</var> file. We then call methods on it
            in our ActionScript by simply calling &quot;myservice.&lt;method&gt;&quot;.
            As an example:
        </p>

        <pre class="programlisting brush: ActionScript">
myservice.hello(&quot;Wade&quot;);
</pre>


        <p class="para">
            When namespacing, you would use
            &quot;myservice.&lt;namespace&gt;.&lt;method&gt;&quot;:
        </p>

        <pre class="programlisting brush: ActionScript">
myservice.world.hello(&quot;Wade&quot;);
</pre>


        <p class="para">
            For more information on Flex RemoteObject invocation, <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html" class="link external">&raquo; 
            visit the Adobe Flex 3 Help site</a>.
        </p>
    </div>

    <div class="section" id="zend.amf.server.errors"><div class="info"><h1 class="title">Error Handling</h1></div>
        

        <p class="para">
            By default, all exceptions thrown in your attached classes or
            functions will be caught and returned as <acronym class="acronym">AMF</acronym> ErrorMessages. However,
            the content of these ErrorMessage objects will vary based on whether
            or not the server is in &quot;production&quot; mode (the default state).
        </p>

        <p class="para">
            When in production mode, only the exception code will be returned.
            If you disable production mode -- something that should be done for
            testing only -- most exception details will be returned: the
            exception message, line, and backtrace will all be attached.
        </p>

        <p class="para">
            To disable production mode, do the following:
        </p>

        <pre class="programlisting brush: php">
$server-&gt;setProduction(false);
</pre>


        <p class="para">
            To re-enable it, pass a <b><tt>TRUE</tt></b> boolean value instead:
        </p>

        <pre class="programlisting brush: php">
$server-&gt;setProduction(true);
</pre>


        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Disable production mode sparingly!</b><br /></span>
            

            <p class="para">
                We recommend disabling production mode only when in development.
                Exception messages and backtraces can contain sensitive system
                information that you may not wish for outside parties to access.
                Even though <acronym class="acronym">AMF</acronym> is a binary format, the specification is now
                open, meaning anybody can potentially deserialize the payload.
            </p>
        </p></blockquote>

        <p class="para">
            One area to be especially careful with is <acronym class="acronym">PHP</acronym> errors themselves.
            When the <span class="property">display_errors</span> <acronym class="acronym">INI</acronym> directive is
            enabled, any <acronym class="acronym">PHP</acronym> errors for the current error reporting level are
            rendered directly in the output -- potentially disrupting the <acronym class="acronym">AMF</acronym>
            response payload. We suggest turning off the <span class="property">display_errors</span>
            directive in production to prevent such problems
        </p>
    </div>

    <div class="section" id="zend.amf.server.response"><div class="info"><h1 class="title">AMF Responses</h1></div>
        

        <p class="para">
            Occasionally you may desire to manipulate the response object
            slightly, typically to return extra message headers. The
             <span class="methodname">handle()</span> method of the server returns the response
            object, allowing you to do so.
        </p>

        <div class="example" id="zend.amf.server.response.messageHeaderExample"><div class="info"><p><b>Example #2 Adding Message Headers to the AMF Response</b></p></div>
            

            <div class="example-contents"><p>
                In this example, we add a &#039;foo&#039; MessageHeader with the value
                &#039;bar&#039; to the response prior to returning it.
            </p></div>

            <pre class="programlisting brush: php">
$response = $server-&gt;handle();
$response-&gt;addAmfHeader(new Zend_Amf_Value_MessageHeader(&#039;foo&#039;, true, &#039;bar&#039;))
echo $response;
</pre>

        </div>
    </div>

    <div class="section" id="zend.amf.server.typedobjects"><div class="info"><h1 class="title">Typed Objects</h1></div>
        

        <p class="para">
            Similar to <acronym class="acronym">SOAP</acronym>, <acronym class="acronym">AMF</acronym> allows passing objects
            between the client and server. This allows a great amount of flexibility and
            coherence between the two environments.
        </p>

        <p class="para">
            <span class="classname">Zend_Amf</span> provides three methods for mapping
            ActionScript and <acronym class="acronym">PHP</acronym> objects.
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    First, you may create explicit bindings at the server level,
                    using the  <span class="methodname">setClassMap()</span> method. The first
                    argument is the ActionScript class name, the second the <acronym class="acronym">PHP</acronym>
                    class name it maps to:
                </p>

                <pre class="programlisting brush: php">
// Map the ActionScript class &#039;ContactVO&#039; to the PHP class &#039;Contact&#039;:
$server-&gt;setClassMap(&#039;ContactVO&#039;, &#039;Contact&#039;);
</pre>

            </li>

            <li class="listitem">
                <p class="para">
                    Second, you can set the public property <var class="varname">$_explicitType</var>
                    in your <acronym class="acronym">PHP</acronym> class, with the
                    value representing the ActionScript class to map to:
                </p>

                <pre class="programlisting brush: php">
class Contact
{
    public $_explicitType = &#039;ContactVO&#039;;
}
</pre>

            </li>

            <li class="listitem">
                <p class="para">
                    Third, in a similar vein, you may define the public method
                     <span class="methodname">getASClassName()</span> in your <acronym class="acronym">PHP</acronym> class;
                    this method should return the appropriate ActionScript class:
                </p>

                <pre class="programlisting brush: php">
class Contact
{
    public function getASClassName()
    {
        return &#039;ContactVO&#039;;
    }
}
</pre>

            </li>
        </ul>

        <p class="para">
            Although we have created the ContactVO on the server we now need to make its
            corresponding class in <acronym class="acronym">AS3</acronym> for the server object to be mapped to.
        </p>

        <p class="para">
            Right click on the src folder of the Flex project and select New -&gt; ActionScript
            File. Name the file ContactVO and press finish to see the new file. Copy the
            following code into the file to finish creating the class.
        </p>

        <pre class="programlisting brush: as">
package
{
    [Bindable]
    [RemoteClass(alias=&quot;ContactVO&quot;)]
    public class ContactVO
    {
        public var id:int;
        public var firstname:String;
        public var lastname:String;
        public var email:String;
        public var mobile:String;
        public function ProductVO():void {
        }
    }
}
</pre>


        <p class="para">
            The class is syntactically equivalent to the <acronym class="acronym">PHP</acronym> of the same name.
            The variable names are exactly the same and need to be in the same case
            to work properly. There are two unique <acronym class="acronym">AS3</acronym> meta tags in this class.
            The first is bindable which makes fire a change event when it is updated.
            The second tag is the RemoteClass tag which defines that this class can
            have a remote object mapped with the alias name in this case
            <em class="emphasis">ContactVO</em>. It is mandatory that this tag the value that was set
            is the <acronym class="acronym">PHP</acronym> class are strictly equivalent.
        </p>

        <pre class="programlisting brush: as">
[Bindable]
private var myContact:ContactVO;

private function getContactHandler(event:ResultEvent):void {
    myContact = ContactVO(event.result);
}
</pre>


        <p class="para">
            The following result event from the service call is cast instantly onto the Flex
            ContactVO. Anything that is bound to myContact will be updated with the returned
            ContactVO data.
        </p>
    </div>

    <div class="section" id="zend.amf.server.resources"><div class="info"><h1 class="title">Resources</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Amf</span> provides tools for mapping resource types
            returned by service classes into data consumable by ActionScript.
        </p>

        <p class="para">
            In order to handle specific resource type, the user needs to create a plugin class named
            after the resource name, with words capitalized and spaces removed (so, resource
            type &quot;mysql result&quot; becomes MysqlResult), with some prefix, e.g.
            <span class="classname">My_MysqlResult</span>. This class should implement one method,
             <span class="methodname">parse()</span>, receiving one argument - the resource - and returning
            the value that should be sent to ActionScript. The class should be located in the file
            named after the last component of the name, e.g. <var class="filename">MysqlResult.php</var>.
        </p>

        <p class="para">
            The directory containing the resource handling plugins should be registered with
            <span class="classname">Zend_Amf</span> type loader:
        </p>

        <pre class="programlisting brush: php">
Zend_Amf_Parse_TypeLoader::addResourceDirectory(
    &quot;My&quot;,
    &quot;application/library/resources/My&quot;
);
</pre>


        <p class="para">
            For detailed discussion of loading plugins, please see
            the <a href="zend.loader.pluginloader.html" class="link">plugin loader</a> section.
        </p>

        <p class="para">
            Default directory for <span class="classname">Zend_Amf</span> resources is registered
            automatically and currently contains handlers for &quot;mysql result&quot; and &quot;stream&quot;
            resources.
        </p>

        <pre class="programlisting brush: php">
// Example class implementing handling resources of type mysql result
class Zend_Amf_Parse_Resource_MysqlResult
{
    /**
     * Parse resource into array
     *
     * @param resource $resource
     * @return array
     */
    public function parse($resource) {
        $result = array();
        while($row = mysql_fetch_assoc($resource)) {
            $result[] = $row;
        }
        return $result;
    }
}
</pre>


        <p class="para">
            Trying to return unknown resource type (i.e., one for which no handler plugin exists)
            will result in an exception.
        </p>
    </div>

    <div class="section" id="zend.amf.server.flash"><div class="info"><h1 class="title">Connecting to the Server from Flash</h1></div>
        

        <p class="para">
            Connecting to your <span class="classname">Zend_Amf_Server</span> from your Flash project is
            slightly different than from Flex. However once the connection Flash functions with
            <span class="classname">Zend_Amf_Server</span> the same way is flex. The following example can
            also be used from a Flex <acronym class="acronym">AS3</acronym> file. We will reuse the same
            <span class="classname">Zend_Amf_Server</span> configuration along with the World class for our
            connection.
        </p>

        <p class="para">
            Open Flash CS and create and new Flash File (ActionScript 3). Name the document
            <var class="filename">ZendExample.fla</var> and save the document into a folder that you will
            use for this example. Create a new <acronym class="acronym">AS3</acronym> file in the same directory
            and call the file <var class="filename">Main.as</var>. Have both files open in your editor. We
            are now going to connect the two files via the document class. Select ZendExample and
            click on the stage. From the stage properties panel change the Document class to Main.
            This links the <var class="filename">Main.as</var> ActionScript file with the user interface
            in <var class="filename">ZendExample.fla</var>. When you run the Flash file ZendExample the
            <var class="filename">Main.as</var> class will now be run. Next we will add ActionScript to
            make the <acronym class="acronym">AMF</acronym> call.
        </p>

        <p class="para">
            We now are going to make a Main class so that we can send the data to the server and
            display the result. Copy the following code into your <var class="filename">Main.as</var> file
            and then we will walk through the code to describe what each element&#039;s role is.
        </p>

        <pre class="programlisting brush: as">
package {
  import flash.display.MovieClip;
  import flash.events.*;
  import flash.net.NetConnection;
  import flash.net.Responder;

  public class Main extends MovieClip {
    private var gateway:String = &quot;http://example.com/server.php&quot;;
    private var connection:NetConnection;
    private var responder:Responder;

    public function Main() {
      responder = new Responder(onResult, onFault);
      connection = new NetConnection;
      connection.connect(gateway);
    }

    public function onComplete( e:Event ):void{
      var params = &quot;Sent to Server&quot;;
      connection.call(&quot;World.hello&quot;, responder, params);
    }

    private function onResult(result:Object):void {
      // Display the returned data
      trace(String(result));
    }
    private function onFault(fault:Object):void {
      trace(String(fault.description));
    }
  }
}
</pre>


        <p class="para">
            We first need to import two ActionScript libraries that perform the bulk of the work.
            The first is NetConnection which acts like a by directional pipe between the client and
            the server. The second is a Responder object which handles the return values from the
            server related to the success or failure of the call.
        </p>

        <pre class="programlisting brush: as">
import flash.net.NetConnection;
import flash.net.Responder;
</pre>


        <p class="para">
            In the class we need three variables to represent the NetConnection, Responder, and
            the gateway <acronym class="acronym">URL</acronym> to our <span class="classname">Zend_Amf_Server</span>
            installation.
        </p>

        <pre class="programlisting brush: as">
private var gateway:String = &quot;http://example.com/server.php&quot;;
private var connection:NetConnection;
private var responder:Responder;
</pre>


        <p class="para">
            In the Main constructor we create a responder and a new connection to the
            <span class="classname">Zend_Amf_Server</span> endpoint. The responder defines two different
            methods for handling the response from the server. For simplicity I have called these
            onResult and onFault.
        </p>

        <pre class="programlisting brush: as">
responder = new Responder(onResult, onFault);
connection = new NetConnection;
connection.connect(gateway);
</pre>


        <p class="para">
            In the onComplete function which is run as soon as the construct has completed we send
            the data to the server. We need to add one more line that makes a call to the
            <span class="classname">Zend_Amf_Server</span> World-&gt;hello function.
        </p>

        <pre class="programlisting brush: as">
connection.call(&quot;World.hello&quot;, responder, params);
</pre>


        <p class="para">
            When we created the responder variable we defined an onResult and onFault function to
            handle the response from the server. We added this function for the successful result
            from the server. A successful event handler is run every time the connection is handled
            properly to the server.
        </p>

        <pre class="programlisting brush: as">
private function onResult(result:Object):void {
    // Display the returned data
    trace(String(result));
}
</pre>


        <p class="para">
            The onFault function, is called if there was an invalid response from the server. This
            happens when there is an error on the server, the <acronym class="acronym">URL</acronym> to the server
            is invalid, the remote service or method does not exist, and any other connection
            related issues.
        </p>

        <pre class="programlisting brush: as">
private function onFault(fault:Object):void {
    trace(String(fault.description));
}
</pre>


        <p class="para">
            Adding in the ActionScript to make the remoting connection is now complete. Running the
            ZendExample file now makes a connection to <span class="classname">Zend_Amf</span>. In review
            you have added the required variables to open a connection to the remote server, defined
            what methods should be used when your application receives a response from the server,
            and finally displayed the returned data to output via  <span class="methodname">trace()</span>.
        </p>
    </div>

    <div class="section" id="zend.amf.server.auth"><div class="info"><h1 class="title">Authentication</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Amf_Server</span> allows you to specify authentication and
            authorization hooks to control access to the services. It is using the infrastructure
            provided by <a href="zend.auth.html" class="link"><span class="classname">Zend_Auth</span></a> and
            <a href="zend.acl.html" class="link"><span class="classname">Zend_Acl</span></a> components.
        </p>

        <p class="para">
            In order to define authentication, the user provides authentication adapter extening
            <span class="classname">Zend_Amf_Auth_Abstract</span> abstract class. The adapter should
            implement the  <span class="methodname">authenticate()</span> method just like regular
            <a href="zend.auth.introduction.html#zend.auth.introduction.adapters" class="link">authentication adapter</a>.
        </p>

        <p class="para">
            The adapter should use properties <em class="emphasis">_username</em> and
            <em class="emphasis">_password</em> from the parent
            <span class="classname">Zend_Amf_Auth_Abstract</span> class in order to authenticate. These
            values are set by the server using  <span class="methodname">setCredentials()</span> method
            before call to  <span class="methodname">authenticate()</span> if the credentials are received
            in the <acronym class="acronym">AMF</acronym> request headers.
        </p>

        <p class="para">
            The identity returned by the adapter should be an object containing property
            <span class="property">role</span> for the <acronym class="acronym">ACL</acronym> access control to work.
        </p>

        <p class="para">
            If the authentication result is not successful, the request is not proceseed further
            and failure message is returned with the reasons for failure taken from the result.
        </p>

        <p class="para">
            The adapter is connected to the server using  <span class="methodname">setAuth()</span> method:
        </p>

        <pre class="programlisting brush: php">
$server-&gt;setAuth(new My_Amf_Auth());
</pre>


        <p class="para">
            Access control is performed by using <span class="classname">Zend_Acl</span> object set by
             <span class="methodname">setAcl()</span> method:
        </p>

        <pre class="programlisting brush: php">
$acl = new Zend_Acl();
createPermissions($acl); // create permission structure
$server-&gt;setAcl($acl);
</pre>


        <p class="para">
            If the <acronym class="acronym">ACL</acronym> object is set, and the class being called defines
             <span class="methodname">initAcl()</span> method, this method will be called with the
            <acronym class="acronym">ACL</acronym> object as an argument. The class then can create additional
            <acronym class="acronym">ACL</acronym> rules and return <b><tt>TRUE</tt></b>, or return
            <b><tt>FALSE</tt></b> if no access control is required for this class.
        </p>

        <p class="para">
            After <acronym class="acronym">ACL</acronym> have been set up, the server will check if access is
            allowed with role set by the authentication, resource being the class name (or
            <b><tt>NULL</tt></b> for
            function calls) and privilege being the function name. If no authentication was
            provided, then if the <em class="emphasis">anonymous</em> role was defined, it will be used,
            otherwise the access will be denied.
        </p>

        <pre class="programlisting brush: php">
if($this-&gt;_acl-&gt;isAllowed($role, $class, $function)) {
    return true;
} else {
    require_once &#039;Zend/Amf/Server/Exception.php&#039;;
    throw new Zend_Amf_Server_Exception(&quot;Access not allowed&quot;);
}
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.amf.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.amf.html">Zend_Amf</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.application.html">Zend_Application</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.amf.html">Zend_Amf</a></li>
  <li><a href="zend.amf.introduction.html">Introduction</a></li>
  <li class="active"><a href="zend.amf.server.html">Zend_Amf_Server</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>